const apiUser = require("../model/apiUsersModel")



const apiUsersController = {
    addUser: async (req, res) => {


        const { username, password, email } = req.body
        await apiUser.create({ username, password, email }).then((result) => {
            res.send("ok")
        }).catch((err) => {
            console.dir(err);
            res.send("on")
        })
    },
    getUser: async (req, res) => {

        const users = await apiUser.find({}, { username: 1, password: 1, email: 1, _id: 1 })
        res.send(users)
    },
    deleteUser: async (req, res) => {
        const users = await apiUser.deleteMany({})
        res.send({ count: users.deletedCount, message: "Deleted all users" })
    },
    deleteByID: async (req, res) => {


        try {
            const user = await apiUser.deleteOne({ _id: req.params.id })
            res.send({ count: user.deletedCount, message: "Deleted user" })

        } catch (error) {
            res.status(500).send({ message: "Error deleting user" })
        }

    },
    updateByID: async (req, res) => {
        const { username, password, email } = req.body
        const user = await apiUser.updateOne({ _id: req.params.id }, { $set: { username, password, email } })
        console.log('await apiUser.count({}):', await apiUser.countDocuments({}));

        (await apiUser.find({}).skip(5).limit(2)).forEach(({ _id, username, password, email }) => {
            // console.log();
            console.log(_id.toString(), username, password, email);
        });

        res.send("ok")
    }







}




module.exports = apiUsersController





